﻿Public Class Form3
    Dim da As New Class1
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim strSql As String = ""
    Dim ct As New System.Globalization.CultureInfo("en-US")

    Private Sub Form3_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Form1.Enabled = True
        strSql = "select * from customer order by cus_id"
        ds = da.PopulateList(strSql)
        dt = ds.Tables(0)
        Form1.DataGridView3.DataSource = dt
        Form1.DataGridView3.AllowUserToResizeRows = False
        For Each column As DataGridViewColumn In Form1.DataGridView3.Columns
            Select Case column.HeaderText
                Case "cus_id"
                    column.HeaderText = "รหัสสมาชิก"
                Case "cus_name"
                    column.HeaderText = "ชื่อสมาชิก"
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                Case "cus_sname"
                    column.HeaderText = "นามสกุล"
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                Case "cus_birthdate"
                    column.HeaderText = "วดป. เกิด"
                Case "cus_permission"
                    column.HeaderText = "สิทธิ(เล่ม)"
                Case "cus_num"
                    column.HeaderText = "ยืม(เล่ม)"
            End Select
        Next
        Form1.DataGridView3.Show()
    End Sub

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = New Point(Form1.Location.X + Form1.Width / 5, Form1.Location.Y + Form1.Height / 5)
        ct.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy"
        System.Threading.Thread.CurrentThread.CurrentCulture = ct
        Form1.Enabled = False
        If Form1.y = False Then
            TextBox1.Text = Form1.DataGridView3.SelectedRows(0).Cells(1).Value
            TextBox2.Text = Form1.DataGridView3.SelectedRows(0).Cells(2).Value
            DateTimePicker1.Value = Form1.DataGridView3.SelectedRows(0).Cells(3).Value
            TextBox3.Text = Form1.DataGridView3.SelectedRows(0).Cells(4).Value
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        DateTimePicker1.Value = DateValue(Now)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If String.IsNullOrEmpty(TextBox1.Text) = True Then
            MsgBox("Please Enter Customer FirstName")
            Exit Sub
        End If
        If String.IsNullOrEmpty(TextBox2.Text) = True Then
            MsgBox("Please Enter Customer SurName Type")
            Exit Sub
        End If
        If String.IsNullOrEmpty(TextBox3.Text) = True Then
            MsgBox("Please Enter Customer Permission Type")
            Exit Sub
        End If
        Dim n As String = DateTimePicker1.Value.Year & "-" & DateTimePicker1.Value.Month & "-" & DateTimePicker1.Value.Day

        If MsgBox("คุณต้องการบันทึกข้อมูลใช่หรือไม่", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            If Form1.y = False Then
                strSql = "update customer set cus_name='" & TextBox1.Text & "',cus_sname='" & TextBox2.Text & "',cus_birthdate='" & n & "',cus_permission='" & TextBox3.Text & "' where cus_id='" & Form1.DataGridView3.SelectedRows(0).Cells(0).Value & "' "
                da.PopulateList(strSql)
                MsgBox("Finished Edit " & TextBox1.Text & " Data")
                Me.Close()
            Else
                strSql = "insert into customer(cus_name,cus_sname,cus_birthdate,cus_permission,cus_num) values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & n & "','" & TextBox3.Text & "','0')"
                da.PopulateList(strSql)
                MsgBox("Finished Add New Data")
                Me.Close()
            End If
        End If
    End Sub
End Class